package com.zhiyizhen.handheld.component.execute.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;

import com.zhiyizhen.handheld.common.DataCache;
import com.zhiyizhen.handheld.common.StringUtil;
import com.zhiyizhen.handheld.common.view.adapter.recyclerview.BaseAdapter;
import com.zhiyizhen.handheld.common.view.adapter.recyclerview.BaseViewHolder;
import com.zhiyizhen.handheld.component.execute.bean.executedetail.GroupListItem;
import com.zhiyizhen.handheld.component.execute.view.ShuyeUnitLayout;
import com.zhiyizhen.handheld.databinding.ItemExecuteShuyeGroupBinding;

public class ExecuteShuyeGroupAdapter extends BaseAdapter<GroupListItem, ExecuteShuyeGroupAdapter.ShuyeGroupViewHolder> {

    public ExecuteShuyeGroupAdapter(Context context) {
        super(context);
    }

    private OnActionClickListener onActionClickListener;

    public void setOnActionClickListener(OnActionClickListener onActionClickListener) {
        this.onActionClickListener = onActionClickListener;
    }

    @NonNull
    @Override
    public ShuyeGroupViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ShuyeGroupViewHolder(this, ItemExecuteShuyeGroupBinding.inflate(LayoutInflater.from(parent.getContext())));
    }

    class ShuyeGroupViewHolder extends BaseViewHolder<GroupListItem> {

        private final ItemExecuteShuyeGroupBinding binding;

        public ShuyeGroupViewHolder(BaseAdapter<GroupListItem, ShuyeGroupViewHolder> adapter, ItemExecuteShuyeGroupBinding viewBinding) {
            super(adapter, viewBinding.getRoot());
            binding = viewBinding;
        }

        @Override
        public void bindData(GroupListItem data) {
            binding.groupNameTv.setText(data.getGroupName());

            binding.usageCodeTextTv.setText(data.getUsageCodeText());
            binding.frequencyCodeTextTv.setText(data.getFrequencyCodeText());

            binding.groupNameTv.setText(data.getGroupName());
            binding.usageCodeTextTv.setText(data.getUsageCodeText());
            binding.frequencyCodeTextTv.setText(data.getFrequencyCodeText());
            binding.dripRateTv.setText(StringUtil.getDripRate(data.getIvDripRate()));

            if (data.getDetailList() != null && !data.getDetailList().isEmpty()) {
                ShuyeUnitLayout shuyeUnitLayout;
                for (int i = 0; i < data.getDetailList().size(); i++) {
                    shuyeUnitLayout = new ShuyeUnitLayout(itemView.getContext(), i + 1, data.getDetailList().get(i), DataCache.getEnumsBean().getEnums().getSkinTestEnum());
                    if (i < data.getDetailList().size() - 1) {
                        shuyeUnitLayout.showDivider();
                    } else {
                        shuyeUnitLayout.hideDivider();
                    }
                    binding.shuyeInfoLayout.addView(shuyeUnitLayout);
                }
            }
            binding.executeBtn.setOnClickListener(v -> {
                if (onActionClickListener != null) {
                    onActionClickListener.onAddExecuteRecord(data);
                }
            });
            if (TextUtils.equals(data.getExecStatus(), "1")) {
                @SuppressLint("DefaultLocale") String result = String.format("第%1$d次/共%2$d次", data.getNextTimes(), data.getTotalTimes());
                binding.timesTv.setText(result);
                binding.dividerExecuteBtn.setVisibility(View.VISIBLE);
                binding.executeBtn.setVisibility(View.VISIBLE);
            } else {
                @SuppressLint("DefaultLocale") String result = String.format("共%1$d次", data.getTotalTimes());
                binding.timesTv.setText(result);
                binding.dividerExecuteBtn.setVisibility(View.GONE);
                binding.executeBtn.setVisibility(View.GONE);
            }

            binding.contentLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

        }
    }

    public interface OnActionClickListener {
        /**
         * 添加执行记录
         */
        void onAddExecuteRecord(GroupListItem data);
    }

}